package lockTest;

import java.util.ArrayList;
import java.util.List;

public class HeapOOM {

    private static int count;

    // -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
    static class OMMObject {

    }

    public static void main(String[] args) {
//		java.lang.OutOfMemoryError: Java heap space
		ommObjectError();

//		java.lang.StackOverflowError
//        getStaticError();
    }

    public static void ommObjectError() {
        List<OMMObject> list = new ArrayList<>();
        while (true) {
            try {
                list.add(new OMMObject());
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void getStaticError() {
        try {
            count++;
            getStaticError();
        } catch (Throwable e) {
            System.out.println(e);
            System.out.println(count);
        }
    }
}
